//
//  NSString+StringTool.m
//  SsehMaster
//
//  Created by 杨阳 on 2018/8/8.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "NSString+StringTool.h"

@implementation NSString (StringTool)

+(BOOL)userInfoEmpty{
    
    if (kStringIsEmpty(DEF_PERSISTENT_GET_OBJECT(SsehUserID))||kStringIsEmpty(DEF_PERSISTENT_GET_OBJECT(SsehUserToken))||kStringIsEmpty(DEF_PERSISTENT_GET_OBJECT(SsehUserAccount))) {
        return YES;
    }
    return NO;
}

+(void)removeUserInfo{
    DEF_PERSISTENT_REMOVE(SsehUserAccount);
    DEF_PERSISTENT_REMOVE(SsehUserID);
    DEF_PERSISTENT_REMOVE(SsehUserToken);
    DEF_PERSISTENT_REMOVE(Sseh_member_phone);
    DEF_PERSISTENT_REMOVE(Sseh_member_img);
    DEF_PERSISTENT_REMOVE(Sseh_member_real_name);
    DEF_PERSISTENT_REMOVE(Sseh_order_count);
}

+(NSString *)emptyStr:(NSString *)string{
    
    return kStringIsEmpty(string)?@"":string;
    
}

//通过日期求星期
+ (NSString*)fromDateToWeek:(NSString*)selectDate
{
    NSInteger yearInt = [selectDate substringWithRange:NSMakeRange(0, 4)].integerValue;
    NSInteger monthInt = [selectDate substringWithRange:NSMakeRange(4, 2)].integerValue;
    NSInteger dayInt = [selectDate substringWithRange:NSMakeRange(6, 2)].integerValue;
    int c = 20;//世纪
    NSInteger y = yearInt -1;//年
    NSInteger d = dayInt;
    NSInteger m = monthInt;
    int w =(y+(y/4)+(c/4)-2*c+(26*(m+1)/10)+d-1)%7;
    NSString *weekDay = @"";
    switch (w) {
        case 0:
            weekDay = @"周日";
            break;
        case 1:
            weekDay = @"周一";
            break;
        case 2:
            weekDay = @"周二";
            break;
        case 3:
            weekDay = @"周三";
            break;
        case 4:
            weekDay = @"周四";
            break;
        case 5:
            weekDay = @"周五";
            break;
        case 6:
            weekDay = @"周六";
            break;
        default:
            break;
    }
    return weekDay;
}

+(NSString *)setNowTimeShow:(NSInteger)timeType andTimeStr:(NSString *)dateString
{
    
    NSArray * dateArr = [dateString componentsSeparatedByString:@"."];
    dateString = [dateArr firstObject];
    NSMutableString * mStr = [[NSMutableString alloc]init];
    for (NSInteger i = 0; i < dateString.length; i++) {
        char c = [dateString characterAtIndex:i];
        if (c>='0' && c <= '9') {
            [mStr appendFormat:@"%c",c];
        }
    }
    dateString = mStr;
    switch (timeType) {
        case 0:
        {
            if (dateString.length<4) {
                return @"";
            }
            NSRange range = NSMakeRange(0, 4);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString;
        }
            break;
        case 1:
        {
            if (dateString.length<6) {
                return @"";
            }
            NSRange range = NSMakeRange(4, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString;
        }
            break;
        case 2:
        {
            if (dateString.length<8) {
                return @"";
            }
            NSRange range = NSMakeRange(6, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString;
        }
            break;
        case 3:
        {
            if (dateString.length<10) {
                return @"";
            }
            NSRange range = NSMakeRange(8, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString;
        }
            break;
        case 4:
        {
            if (dateString.length < 12) {
                return @"";
            }
            NSRange range = NSMakeRange(10, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString;
        }
            break;
        case 5:
        {
            if (dateString.length<14) {
                return @"";
            }
            NSRange range = NSMakeRange(12, 2);
            NSString *yearString = [dateString substringWithRange:range];
            return yearString;
        }
            break;
        default:
            break;
    }
    return @"";
}

@end
